package swu.cx.androidmvc

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import swu.cx.androidmvc.Model.Book
import swu.cx.androidmvc.Utils.BookProviderFactory

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        submit.setOnClickListener {
            if (bookName.text.isEmpty()){
                Toast.makeText(this,"书名不能为空！！",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (athorName.text.isEmpty()){
                Toast.makeText(this,"作者名不能为空！！",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            //保存书本信息
            val book = Book(bookName.text.toString(),athorName.text.toString())
            //获取仓库对象
            val repository = BookProviderFactory.getBookRepository()
            repository.addBook(book,this)
        }
    }
    fun upDateUI(str:String){
        content.text = str
        bookName.setText("")
        athorName.setText("")
    }
}